﻿using Newtonsoft.Json.Linq;
using System;
using System.Windows.Forms;

namespace 云起停车_岗亭端
{
    public partial class Form1 : Form
    {
        private static Form1 from1 = null;

        private Form1()
        {
            InitializeComponent();
            textBox1.Enter += new EventHandler(this.textBox1_Enter);
            textBox1.Leave += new EventHandler(this.textBox1_Leave);
            textBox2.Enter += new EventHandler(this.textBox2_Enter);
            textBox2.Leave += new EventHandler(this.textBox2_Leave);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.reset();
        }

        public static Form1 getInstance()
        {
            if (from1 != null)
            {
                return from1;
            }
            from1 = new Form1();
            return from1;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox2.Text == null || this.textBox2.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入岗亭序列号");
                return;
            }
            if (this.textBox1.Text == "请输入密码" || this.textBox1.Text==null || this.textBox1.Text.Trim().Equals("")) {
                MessageBox.Show("请输入岗亭密码");
                return;
            }
            JObject obj = new JObject();
            obj.Add("uniqid", this.textBox2.Text.Trim());
            obj.Add("password", this.textBox1.Text);
            Response response =await Utils.PostAsync("sentrybox/index/login", obj, this);
            if (null == response) {
                return;
            }
            RtspPlayer.Properties.Settings.Default.序列号 = this.textBox2.Text.Trim();
            RtspPlayer.Properties.Settings.Default.Save();
            this.reset();
            Form2 form2 = Form2.getInstance();
            form2.Show();
            this.Hide();
        }

        private void textBox1_Enter(object sender, System.EventArgs e)
        {
            if (this.textBox1.Text == "请输入密码")
            {
                this.textBox1.Text = "";
                this.textBox1.PasswordChar = '*';
                this.textBox1.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void textBox1_Leave(object sender, System.EventArgs e)
        {
            if (this.textBox1.Text == "")
            {
                this.reset();
            }
        }

        private void textBox2_Enter(object sender, System.EventArgs e)
        {
            if (this.textBox2.Text == "请输入岗亭序列号")
            {
                this.textBox2.Text = "";
                this.textBox2.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void textBox2_Leave(object sender, System.EventArgs e)
        {
            if (this.textBox2.Text == "")
            {
                this.textBox2.Text = "请输入岗亭序列号";
                this.textBox2.ForeColor = System.Drawing.Color.Gray;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.reset();
        }

        private void reset()
        {
            string uniqid = RtspPlayer.Properties.Settings.Default.序列号;
            if (uniqid == null || uniqid.Trim().Equals(""))
            {
                this.textBox2.Text = "请输入岗亭序列号";
                this.textBox2.ForeColor = System.Drawing.Color.Gray;
                this.textBox2.Enabled = true;
                this.button3.Visible = false;
            }
            else 
            {
                this.textBox2.Text = uniqid;
                this.textBox2.Enabled = false;
                this.button3.Visible = true;
            }
            this.textBox1.Text = "请输入密码";
            this.textBox1.PasswordChar = '\0';
            this.textBox1.ForeColor = System.Drawing.Color.Gray;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("您确定要重置序列号吗，可能会导致查看不了监控？", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                RtspPlayer.Properties.Settings.Default.序列号 = "";
                RtspPlayer.Properties.Settings.Default.Save();
                this.reset();
            }
        }
    }
}
